using System;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using LibreriaCommon;
using LibreriaData;
using SistemaControlTraficoLibDS;

namespace SistemaControlTraficoLibData
{
    public abstract class daReservaBase : daBase, IDisposable
    {
        public daReservaBase(SecurityInfo securityInfo) : base(securityInfo)
        {
        }

        #region Dispose
        private bool disposed = false;

        public virtual void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    //Liberar recursos manejados
                }
                //liberar recursos no manejados
            }
            disposed = true;
        }

        ~daReservaBase()
        {
            Dispose(false);
        }
        #endregion Dispose

        public virtual dsReserva FindByPrimaryKey(int ReservaID)
        {
            try
            {
                dsReserva ds = new dsReserva();
                daCommon.GenericFind(securityInfo, "ReservaFindByPrimaryKey", ds, new string[] {ds.Reserva.TableName}, ReservaID);
                return ds;
            }
            catch (Exception ex)
            {
                throw new daException(ex);
            }
        }

        public virtual dsReserva.ReservaRow FindRow(int ReservaID)
        {
            try
            {
                return FindByPrimaryKey(ReservaID).Reserva[0];
            }
            catch (Exception ex)
            {
                throw new daException(ex);
            }
        }

        public virtual dsReserva FindByEstado(object estado)
        {
            try
            {
                dsReserva ds = new dsReserva();
                daCommon.GenericFind(securityInfo, "ReservaFindByEstado", ds, new string[] {ds.Reserva.TableName}, estado);
                return ds;
            }
            catch (Exception ex)
            {
                throw new daException(ex);
            }
        }

        public virtual void Update(dsReserva ds)
        {
            Database connection = null;
            DbCommand insertCommand = null;
            DbCommand updateCommand = null;
            DbCommand deleteCommand = null;
            
            try
            {
                string[] parameters = new string[] { "ReservaID", "ClienteID", "TipoMovilID", "ReservaFecha", "PartidoSalidaID", "LocalidadSalidaID", "CalleSalidaID", "ReservaCalleAlturaSalida", "ReservaPisoSalida", "ReservaDepartamentoSalida", "PartidoLlegadaID", "LocalidadLlegadaID", "CalleLlegadaID", "ReservaCalleAlturaLlegada", "ReservaPisoLlegada", "ReservaDepartamentoLlegada", "ReservaTiempoEstimado", "ReservaCostoEstimado", "ReservaObservaciones", "Activo", "FechaAlta", "UsuarioAlta", "FechaModificacion", "UsuarioModificacion" };

                connection = daConnection.GetOpenedConnection();
                insertCommand = connection.GetStoredProcCommandWithSourceColumns("ReservaInsert", parameters);
                updateCommand = connection.GetStoredProcCommandWithSourceColumns("ReservaUpdate", parameters);
                deleteCommand = connection.GetStoredProcCommandWithSourceColumns("ReservaDelete", "ReservaID");
                
                daCommon.UpdateDataset(securityInfo, insertCommand, deleteCommand, updateCommand, ds, ds.Reserva.TableName);
                ds.AcceptChanges();
            }
            catch (Exception ex)
            {
                throw new daException(ex);
            }
            finally
            {
                if (insertCommand != null)
                {
                    insertCommand.Dispose();
                }

                if (updateCommand != null)
                {
                    updateCommand.Dispose();
                }

                if (deleteCommand != null)
                {
                    deleteCommand.Dispose();
                }

            }
        }

    }
}

